package edu.city.class_2201_sno_202212008.Activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.WindowManager;
import android.widget.LinearLayout;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import com.bumptech.glide.Glide;
import edu.city.class_2201_sno_202212008.Adapter.LeaderAdapter;
import edu.city.class_2201_sno_202212008.Domain.UserModel;

import edu.city.class_2201_sno_202212008.R;
import edu.city.class_2201_sno_202212008.databinding.ActivityLeaderBinding;
import java.util.ArrayList;
import java.util.List;

public class LeaderActivity extends AppCompatActivity {
    private ActivityLeaderBinding binding;
    private LeaderAdapter leaderAdapter;
    private List<UserModel> weeklyData;
    private List<UserModel> monthlyData;
    private List<UserModel> allTimeData;
    private int currentTab = 0; // 0: Weekly, 1: Monthly, 2: All Time

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityLeaderBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        getWindow().setFlags(
                WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS,
                WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS
        );

        leaderAdapter = new LeaderAdapter();
        initializeData();
        setupButtons();
        loadWeeklyData(); // 默认显示周榜

        binding.bottomMenu.setItemSelected(R.id.Board, true);
        binding.bottomMenu.setOnItemSelectedListener(itemId -> {
            if (itemId == R.id.home) {
                startActivity(new Intent(LeaderActivity.this, MainActivity.class));
            }
            return null;
        });

        binding.leaderView.setLayoutManager(new LinearLayoutManager(this));
        binding.leaderView.setAdapter(leaderAdapter);
    }

    private void initializeData() {
        // 周榜数据 - 模拟最近一周的活跃玩家
        weeklyData = new ArrayList<>();
        weeklyData.add(new UserModel(1, "Alex", "person1", 4850));
        weeklyData.add(new UserModel(2, "Emma", "person2", 4560));
        weeklyData.add(new UserModel(3, "Lucas", "person3", 3873));
        weeklyData.add(new UserModel(4, "Sophia", "person4", 3250));
        weeklyData.add(new UserModel(5, "Mia", "person5", 3015));
        weeklyData.add(new UserModel(6, "Noah", "person6", 2970));
        weeklyData.add(new UserModel(7, "Ava", "person7", 2870));
        weeklyData.add(new UserModel(8, "Ethan", "person8", 2670));
        weeklyData.add(new UserModel(9, "Isabella", "person9", 2380));
        weeklyData.add(new UserModel(10, "William", "person10", 2280));

        // 月榜数据 - 模拟最近一个月的玩家
        monthlyData = new ArrayList<>();
        monthlyData.add(new UserModel(1, "Sophia", "person1", 5200));
        monthlyData.add(new UserModel(2, "Daniel", "person2", 4800));
        monthlyData.add(new UserModel(3, "James", "person3", 4200));
        monthlyData.add(new UserModel(4, "John Smith", "person4", 3800));
        monthlyData.add(new UserModel(5, "Emily Johnson", "person5", 3500));
        monthlyData.add(new UserModel(6, "David Brown", "person6", 3200));
        monthlyData.add(new UserModel(7, "Sarah Wilson", "person7", 3100));
        monthlyData.add(new UserModel(8, "Michael Davis", "person8", 2900));
        monthlyData.add(new UserModel(9, "Lisa Anderson", "person9", 2700));
        monthlyData.add(new UserModel(10, "Robert Taylor", "person10", 2500));

        // 总榜数据 - 模拟历史总排行榜
        allTimeData = new ArrayList<>();
        allTimeData.add(new UserModel(1, "Legend", "person1", 8500));
        allTimeData.add(new UserModel(2, "Champion", "person2", 7800));
        allTimeData.add(new UserModel(3, "Master", "person3", 7200));
        allTimeData.add(new UserModel(4, "Pro", "person4", 6800));
        allTimeData.add(new UserModel(5, "Elite", "person5", 6500));
        allTimeData.add(new UserModel(6, "Veteran", "person6", 6200));
        allTimeData.add(new UserModel(7, "Expert", "person7", 5900));
        allTimeData.add(new UserModel(8, "Advanced", "person8", 5600));
        allTimeData.add(new UserModel(9, "Intermediate", "person9", 5300));
        allTimeData.add(new UserModel(10, "Beginner", "person10", 5000));
    }

    private void setupButtons() {
        // Weekly按钮
        binding.button1.setOnClickListener(v -> {
            if (currentTab != 0) {
                currentTab = 0;
                updateButtonStyles();
                loadWeeklyData();
            }
        });

        // Monthly按钮
        binding.button2.setOnClickListener(v -> {
            if (currentTab != 1) {
                currentTab = 1;
                updateButtonStyles();
                loadMonthlyData();
            }
        });

        // All Time按钮
        binding.button3.setOnClickListener(v -> {
            if (currentTab != 2) {
                currentTab = 2;
                updateButtonStyles();
                loadAllTimeData();
            }
        });
    }

    private void updateButtonStyles() {
        // 重置所有按钮样式
        binding.button1.setBackgroundResource(R.drawable.white_bg);
        binding.button1.setTextColor(getResources().getColor(R.color.navy_bule));
        binding.button2.setBackgroundResource(R.drawable.white_bg);
        binding.button2.setTextColor(getResources().getColor(R.color.navy_bule));
        binding.button3.setBackgroundResource(R.drawable.white_bg);
        binding.button3.setTextColor(getResources().getColor(R.color.navy_bule));

        // 设置当前选中按钮的样式
        switch (currentTab) {
            case 0: // Weekly
                binding.button1.setBackgroundResource(R.drawable.blue_bg);
                binding.button1.setTextColor(getResources().getColor(R.color.white));
                break;
            case 1: // Monthly
                binding.button2.setBackgroundResource(R.drawable.blue_bg);
                binding.button2.setTextColor(getResources().getColor(R.color.white));
                break;
            case 2: // All Time
                binding.button3.setBackgroundResource(R.drawable.blue_bg);
                binding.button3.setTextColor(getResources().getColor(R.color.white));
                break;
        }
    }

    private void loadWeeklyData() {
        updateTopThree(weeklyData);
        List<UserModel> list = weeklyData.subList(3, weeklyData.size());
        leaderAdapter.getDiffer().submitList(list);
    }

    private void loadMonthlyData() {
        updateTopThree(monthlyData);
        List<UserModel> list = monthlyData.subList(3, monthlyData.size());
        leaderAdapter.getDiffer().submitList(list);
    }

    private void loadAllTimeData() {
        updateTopThree(allTimeData);
        List<UserModel> list = allTimeData.subList(3, allTimeData.size());
        leaderAdapter.getDiffer().submitList(list);
    }

    private void updateTopThree(List<UserModel> data) {
        // 更新前三名数据
        binding.scoreTxt1.setText(String.valueOf(data.get(0).getScore()));
        binding.scoreTxt2.setText(String.valueOf(data.get(1).getScore()));
        binding.scoreTxt3.setText(String.valueOf(data.get(2).getScore()));
        binding.titleTop1Txt.setText(data.get(0).getName());
        binding.titleTop2Txt.setText(data.get(1).getName());
        binding.titleTop3Txt.setText(data.get(2).getName());

        // 加载前三名头像
        loadTopThreeImages(data);
    }

    private void loadTopThreeImages(List<UserModel> data) {
        int drawableResourceId1 = binding.getRoot().getResources().getIdentifier(
                data.get(0).getPic(), "drawable", binding.getRoot().getContext().getPackageName()
        );
        int drawableResourceId2 = binding.getRoot().getResources().getIdentifier(
                data.get(1).getPic(), "drawable", binding.getRoot().getContext().getPackageName()
        );
        int drawableResourceId3 = binding.getRoot().getResources().getIdentifier(
                data.get(2).getPic(), "drawable", binding.getRoot().getContext().getPackageName()
        );

        if (drawableResourceId1 != 0) {
            Glide.with(binding.getRoot().getContext())
                    .load(drawableResourceId1)
                    .into(binding.pic1);
        } else {
            binding.pic1.setImageResource(R.drawable.ic_launcher_background);
        }
        if (drawableResourceId2 != 0) {
            Glide.with(binding.getRoot().getContext())
                    .load(drawableResourceId2)
                    .into(binding.pic2);
        } else {
            binding.pic2.setImageResource(R.drawable.ic_launcher_background);
        }
        if (drawableResourceId3 != 0) {
            Glide.with(binding.getRoot().getContext())
                    .load(drawableResourceId3)
                    .into(binding.pic3);
        } else {
            binding.pic3.setImageResource(R.drawable.ic_launcher_background);
        }
    }
} 